package com.demo.controller;

import com.demo.entity.Department;
import com.demo.entity.Result;
import com.demo.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/user/department")
@Slf4j
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @PostMapping("/save")
    public Result save(@RequestBody Department department){
        if (department!=null){
            try{
                if (departmentService.save(department)==1)
                    return Result.success();
                else return Result.error("保存失败");
            }catch (Exception e){
                Result.error("保存失败");
            }

        }
        return Result.error("重复部门编号");
    }
    @PostMapping("/delete")
    public Result delete(@RequestBody Department department){
        int id = department.getId();
        if (id!=0){
            if (departmentService.delete(id)==1){
                departmentService.remakeDepartment();
                return Result.success();
            }
            else return Result.error("删除失败");
        }
        return Result.error("删除失败");
    }
    @GetMapping("/deleteAll")
    public Result deleteAll(){
        if (departmentService.deleteAll()>0)
            return Result.success();
        return Result.error("删除失败");
    }
    @PostMapping("/modify")
    public Result modify(@RequestBody Department department){
        try {
            if (departmentService.modify(department)==1)
                return Result.success();
        }catch (Exception e){
            Result.error("重复部门编号");
        }
        return Result.error("修改失败，重复部门编号");
    }
    @GetMapping("/getOne")
    public Result getOne(Integer id){
        Department department = departmentService.getOne(id);
        if (department!=null)
            return Result.success(department);
        return Result.error("查询失败");
    }
    @GetMapping("/getAll")
    public Result getAll(){
        List<Department> list= departmentService.getAll();
        if (list.size()>0)
            return Result.success(list);
        return Result.error("查询失败");
    }
    @GetMapping("/findByPage")
    public Result findByPage(Integer pageNow, Integer pageSize) {
        Map<String, Object> result = new HashMap<>();
        pageNow = pageNow == null ? 1 : pageNow; // 不传当前页数默认为1
        pageSize = pageSize == null ? 4 : pageSize; // 不传当前页面显示条数默认显示4条
        List<Department> list = departmentService.findByPage(pageNow,pageSize); // 分页查询
        int totals = departmentService.findTotals();
        result.put("list", list);
        result.put("total", totals);
        if (result.size()>0)
            return Result.success(result);
        return Result.error("查询失败");
    }
    @GetMapping("/findList")
    public Result findList(){
        List<Department> list = departmentService.treeList();
        if (list.size()>0)
            return Result.success(list);
        else return Result.error("查询失败");
    }
}
